'use strict'
// 微信自动回复的内容处理

const log = require('../log.js')

var path = require('path')

// var wechatApi = new Wechat(config.wechat)  //初始化，配置参数，生成实例
var wx = require('./index')
var wechatApi = wx.getWechat()

exports.reply = function *(next){
	var message = this.weixin
 console.log(message)
	if(message.MsgType === 'event'){
		if(message.Event === 'subscribe'){
			if(message.EventKey){
				console.log('扫二维码进来：' + message.EventKey + '' + message.ticket)
			}

			this.body = '你好，感谢您的订阅 \n' +
			'回复 1 ~ 3，测试文字回复 \n' +
			'回复 4，测试图文回复 \n' +
			'回复 首页，进入电影首页 \n' +
			'回复 登录，进入微信登录绑定 \n' +
			'回复 游戏，进入游戏页面 \n' +
			'回复 电影名字，查询电影信息 \n' +
			'回复 语音，查询电影信息 \n' +
			'也可以点击 <a href="http://hmsqdr.natappfree.cc/daojian">到件查询</a>'
		}
		else if(message.Event === 'unsubscribe'){
			console.log('欢迎再来！')
			this.body = ''
		}
		else if(message.Event === 'LOCATION'){
			this.body = '您上报的未知是：' + message.latitude + '/' + message.Longitude + '-' + message.Precision
		}
		else if(message.Event === 'CLICK'){
			this.body = '您点击了菜单：' + message.EventKey
		}
		else if(message.Event === 'SCAN'){
			console.log('关注后的二维码' + message.EventKey + ' ' + message.Ticket)

			this.body = '看到您扫了一下哦'
		}
		else if(message.Event === 'VIEW'){
			this.body = '您点击了菜单中的链接：' + message.EventKey
		}
	}
	else if(message.MsgType === 'text'){
		var content = message.Content
		var reply = '额，你说的 ' + message.Content + ' 太复杂啦'

		if(content === '1'){
			reply = '天下第一是大米'
		}
		else if( content === '2'){
			reply = '天下第二吃豆腐'	
		}
		else if( content === '3'){
			reply = '天下第三吃仙丹'	
		}
		else if( content === '4'){
			reply = [{
				title: '技术改变世界',
				description: '这里是描述',
				picurl: 'http://pic33.nipic.com/20130907/13534366_092511672176_2.jpg',
				url: 'https//www.baidu.com'
			},
			{
				title: '第二条表体',
				description: '这里是描述',
				picurl: 'http://pic23.photophoto.cn/20120530/0020033092420808_b.jpg',
				url: 'https//www.baidu.com'
			}]	

		}
		else if( content === '5'){
			var data = yield wechatApi.uploadMaterial('image', __dirname + '/2.jpg')
			
			reply = {
				type: 'image',
				mediaId: data.media_id
			}
		}
		else if( content === '6'){
			var data = yield wechatApi.uploadMaterial('video', __dirname + '/3.mp4')
			
			reply = {
				type: 'video',
				title: '回复的视频内容',
				description: '这个就是描述',
				mediaId: data.media_id
			}
		}
		else if( content === '7'){ 
			var data = yield wechatApi.uploadMaterial('image', __dirname + '/2.jpg')
			//未测试
			reply = {
				type: 'music',
				title: '回复音乐内容',
				description: '放松一下',
				musicUrl: 'http://mpge.5nd.com/2015/2015-9-12/66325/1.mp3',
				ThumbMediaId: data.media_id
			}
		}
		else if( content === '8'){ //新增永久图片素材
			var data = yield wechatApi.uploadMaterial('image', __dirname + '/4.jpg', {type: 'image'})
			
			reply = {
				type: 'image',
				mediaId: data.media_id
			}
		}
		else if( content === '9'){ //上传永久视频素材
			var data = yield wechatApi.uploadMaterial('video', __dirname + '/3.mp4', {type: 'video', description: '{"title": "这是一个永久视频的标题", "introduction": "这是啥东西？"}'})
			
			log.info(data)

			reply = {
				type: 'video',
				title: '回复的视频内容',
				description: '这个就是描述',
				mediaId: data.media_id
			}
			
		}
		else if( content === '10'){ //测试之前写的几个接口是否能正常工作
			//获取图片路径,传的是永久素材
			var picData = yield wechatApi.uploadMaterial('image', __dirname + '/2.jpg', {})

			var media = {
				articles:[{
					title: 'tututu',
					thumb_media_id: picData.media_id,
					show_cover_pic: 1,
					author: 'lvj',
					digest: '没有摘要',
					content: '没有内容',
					// url: '',
					content_source_url: 'https//github.com' 	//“阅读原文”的链接路径
				}]
			}

			//上传永久图文素材
			data = yield wechatApi.uploadMaterial('news', media, {})
			log.info(data.media_id)
			//获取永久素材,根据data.media_id 素材ID找到图文数据
			data = yield wechatApi.fetchMaterial(data.media_id, 'news',{})

log.info(data)
			//为了回复拿到的图文信息
			var items = data.news_item
			var news = []

			items.forEach(function(item){
				news.push({
					title: item.title,
					description: item.digest,
					picUrl: picData.url,
					url: item.url
				})
			})

			reply = news
		}
		else if( content === '11'){ //获取素材数量
			var counts = yield wechatApi.countMaterial()
			
			// log.info(JSON.stringify(counts))

			var results = yield [
				wechatApi.batchMaterial({
					type: 'image',
					offset: 0,
					counts: 10
				}),
				// wechatApi.batchMaterial({
				// 	type: 'video',
				// 	offset: 0,
				// 	counts: 10
				// })
				
			]
			reply = 'image=' + counts.image_count
			
		}
		else if( content === '12'){ //获取永久素材
			var counts = yield wechatApi.countMaterial()
			
			// log.info(JSON.stringify(counts))

			var results = yield [
				wechatApi.batchMaterial({
					type: 'image',
					offset: 0,
					counts: 10
				})
				
			]

			var items = results[0].item
log.info(items)

			items.forEach(function(item){
				var media = item.media_id
log.info(item)
			})

log.info(media)
			data = yield wechatApi.fetchMaterial(media, 'news', {})
log.info(data)			
			//为了回复拿到的图文信息
			var items = data.news_item
			var news = []

			items.forEach(function(item){
				news.push({
					title: item.title,
					description: item.digest,
					picUrl: picData.url,
					url: item.url
				})
			})

			reply = news
			
		}
		else if(content === '100'){

			//获取用户列表
			var group11 = yield wechatApi.fetchUserList()
			log.info(group11)			

			//获取当前公众号的所有标签
			var groups = yield wechatApi.fetchTags()
			log.info(groups)			

			reply = 'Tags done!'
		}
		else if(content === '200'){
			var tplId = 'R07SApTSSnG1l-MuR1QrJLigzBwicGHdbyOx8Rbson4'
			var url = 'http://weixin.qq.com/download'
			var _data = {
						
					}
					// message.FromUserName
			var data = yield wechatApi.sendMessageTpl(message.FromUserName, tplId, url)
			log.info(data)	
			reply = '消息模板发送成功'		
		}

		this.body = reply
		// log.info(this.body)
	}

	yield next
}